<?php
/**
 * Created by PhpStorm.
 * User: fengke
 * Date: 2018/12/10
 * Time: 17:03
 */
namespace app\common\library\book;
//丹书铁卷
use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;
use QL\QueryList;

class dstiejuanCollect
{
    private $_config;
    
    public function __construct()
    {
        $this->_config = config('collect.dstiejuan');
    }

    /**
     * 函数用途描述: 书籍搜索
     * @author: Feng_Ke
     * @date: 2018/12/10 17:17
     */
    public function getBookSearch($searchKey){
        $url = $this->_config['search'];
        $QueryList = new  QueryList();
        $rules = [
            // 采集文章的id
            'book_id' => ['a','href'],
            // 采集文章的封面
            'img' => ['img','src'],
            // 采集文章标题
            'title' => ['.bookname','text'],
            // 采集文章作者
            'author' => ['.author','html'],
            //采集分类
            'class' => ['.data','text'],
            // 采集文章内容
            'intro' => ['.intro','text']
        ];
        $range = '.list>li';
        $rt = $QueryList->post($url,['searchkey' => $searchKey])->rules($rules)->range($range)->query()->getData()->all();
        foreach ($rt as $key=>$value){
            $rt[$key]['collection_id'] = 1;
            $rt[$key]['author'] = explode('<',$value['author'])[0];
            $rt[$key]['book_id'] = explode('/',explode('.',$value['book_id'])[0])[2];
        }
        return $rt;
    }

    /**
     * 函数用途描述:得到数据首页详情
     * @author: Feng_Ke
     * @date: 2018/12/14 11:05
     */
    public function getBookInfo($book_id){
        $url = sprintf($this->_config['book'],$book_id);
        try{
            $QueryList = new  QueryList();
            $ql = $QueryList::get($url);
            $data = [];
            $data['book_status'] = $ql->find('meta')->eq(20)->content;
            $data['book_update_time'] = $ql->find('meta')->eq(22)->content;
            $data['latest_chapter_name'] = $ql->find('meta')->eq(23)->content;
            print_r($data);

            //查找章节列表
            // 定义采集规则
            $rules = [
                // 目录链接
                'chapter_url' => ['a','href'],
                // 目录名称
                'chapter_name' => ['a','text'],
                //上一页
                //下一页

            ];
            $range = '.vlist>li';
            $chapter = $ql->rules($rules)->range($range)->query()->getData();
            var_dump($chapter);
            die;
        }catch(RequestException $e){
            //print_r($e->getRequest());
            echo 'Http Error'.$e->getMessage();
        }
    }

    /**
     * 函数用途描述: 获取书籍的章节目录
     * @author: Feng_Ke
     * @date: 2018/12/14 15:41
     */
    public function getBookChapter($book_id){
        $QueryList = new  QueryList();
        $url = sprintf($this->_config['directory'],$book_id);
        try{
            $ql = $QueryList::get($url);
//            //查找章节列表
//            // 定义采集规则
            $rules = [
                //章节id
                'chapter_id' => ['.read>li','chapter-id'],
                // 目录链接
                'chapter_url' => ['.read>li>a','href'],
                // 目录名称
                'chapter_name' => ['.read>li>a','text'],
            ];
            $data['chapter'] = $ql->rules($rules)->query()->queryData();
            //上一页
            //下一页
            //正序
            //倒叙

            var_dump($data);
            die;
        }catch(RequestException $e){
            //print_r($e->getRequest());
            echo 'Http Error'.$e->getMessage();
        }
    }
}